package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.thirdparty.PlatformMgr;
import com.yanqu.road.server.pb.UnionPb;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;


//@Cmd(code = Protocol.S_UNION_MODIFY, desc = "修改联盟信息")
public class UnionModifyCmd extends AbstractCommand {
    private int type;         //0：联盟名称； 1：微信号； 2：qq号； 3：对外公告； 4：对内公告
    private String content;
    private boolean needResp = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionProto.UnionModifyReqMsg reqMsg = UnionProto.UnionModifyReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        content = reqMsg.getContent();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if (type == 0 || type == 3) {
            /**
             * 送财功能被禁用 则返回系统未开放
             */
            if(ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_UNION_MODIFY)) {
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                needResp = true;
                return;
            }
        }

        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        if((0 == type || 3 == type) && //改商会名或者改商会公告
                eChannelType.isMainLandChannel(channelId) && ChannelConfig.CN_FORBIDDEN_WORD_CHECK == 1) {//大陆渠道，实时检测内容是否屏蔽
            PlatformMgr.addUnionModifyTask(player, type, content);
        }else {
            needResp = true;
            ret = PlatformMgr.unionModify(player, type, content);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(needResp) {
            UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
            respMsg.setRet(ret);
            if (ret == 0) {
                UnionInfo unionInfo = UnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
                UnionProto.UnionTempMsg.Builder unionMsg = UnionPb.parseUnionTempMsg(unionInfo);
                if (null != unionMsg) {
                    respMsg.setUnion(unionMsg);
                }
            }
            player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);
        }
    }
}
